package com.tuanzi.loan.web.bean.channel;

import com.google.common.collect.Lists;
import com.tuanzi.loan.business.entity.common.CommProductChannel;
import com.tuanzi.loan.business.repository.common.impl.SecondaryChannelGroupRepository;
import com.tuanzi.loan.business.service.common.CommProductChannelService;
import com.tuanzi.loan.core.aspect.Debug;
import com.tuanzi.loan.vo.request.channel.ChannelSearchRequest;
import com.tuanzi.loan.vo.response.channel.ChannelSearchResponse;
import com.tuanzi.loan.vo.response.channel.SecondaryChannelGroupResponse;
import com.tuanzi.loan.web.configuration.JSFListingSessionBean;
import com.tuanzi.loan.web.lazy.channel.ChannelLazyDataModel;
import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.faces.bean.ViewScoped;
import java.util.List;
import java.util.stream.Collectors;

import static com.tuanzi.loan.business.constants.DictionaryCode.CHANNEL_GROUP;

/**
 * @author Ray 2017/09/21
 */
@Debug
@Component
@ViewScoped
public class ChannelListBean extends JSFListingSessionBean<ChannelSearchRequest, ChannelSearchResponse> {

    private static final long serialVersionUID = -2152778554108785342L;

    @Autowired
    private CommProductChannelService commProductChannelService;

    @Autowired
    private SecondaryChannelGroupRepository secondaryChannelGroupRepository;

    private List<SecondaryChannelGroupResponse> secondaryChannelGroups;

    public void onReset() {
        initLazyDataModel();
    }

    public String onAdd() {
        return redirect("/views/channel/channel/info.xhtml");
    }

    public String onAddSecondaryChannelGroup() {
        return redirect("/views/channel/channel/add-secondary.xhtml");
    }

    public String onEdit(String id) {
        return redirect("/views/channel/channel/info?id=" + id);
    }

    private List<CommProductChannel> getSelectedDictionaries() {
        return commProductChannelService
                .findAll(selectedRecords.parallelStream().map(ChannelSearchResponse::getId).map(String::valueOf).map(NumberUtils::toLong).collect(Collectors.toList()));
    }

    @Override
    protected String page() {
        return "/views/channel/channel/list.xhtml";
    }

    @Override
    protected void initLazyDataModel() {
        $ = new ChannelSearchRequest();
        lazyDataModel = new ChannelLazyDataModel($, session, commProductChannelService);
        secondaryChannelGroups = secondaryChannelGroupRepository.findAll();
    }

    @Override
    protected List<String> getDictionaryCodes() {
        return Lists.newArrayList(CHANNEL_GROUP);
    }

    public String translateSecondaryChannelGroup(Integer secondaryChannelGroupId) {
        return secondaryChannelGroupRepository.findNameById(secondaryChannelGroupId);
    }

    public void onChangeChannelGroup() {
        if ($.getChannelGroup() == null) {
            secondaryChannelGroups = secondaryChannelGroupRepository.findAll();
        } else {
            secondaryChannelGroups = secondaryChannelGroupRepository.findByPrimaryChannelGroup($.getChannelGroup());
        }
    }

    public List<SecondaryChannelGroupResponse> getSecondaryChannelGroups() {
        return secondaryChannelGroups;
    }

}
